package cn.zahou.SJR.Spring_Template;
import java.io.*;
import cn.zahou.SJR.JDBCToolsc.Jdbc_toolsc;
import cn.zahou.SJR.domain.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.List;
import java.util.Map;
public class blackBoxTest_Template {
    private  JdbcTemplate template=new JdbcTemplate(Jdbc_toolsc.getDataSource());
//    增数据
    @Test
    public  void Tes1(){
            String sql="insert into emp values(null,'菊花虾','男',4444.00,'2000-12-1',1)";
            int count=template.update(sql);
            System.out.println("本次影响库内记录"+count+"条.");
    }
//    删数据
    @Test
    public  void  Tes2(){
        String sql="delete  from emp where name ='燕赤霞'";
        int count=template.update(sql);
        System.out.println("本次影响库内记录"+count+"条.");
    }
//    改数据
    @Test
    public  void Test3() {
//1.导入5个jar
//            2.创建JDBCTemplate对象

        String sql="update emp set salary=9889.00 where name=?";
        int count=template.update(sql,"白骨精");
//        期望值判断
        assert count==1;
        System.out.println("本次影响库内记录"+count+"条.");
    }
    //    查数据
    @Test
    public void test5() throws IOException {
        String sql = "select * from emp";
//        queryForList返回列表
        BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));

        List<Map<String, Object>> list = template.queryForList(sql);
        for (Map<String, Object> stringObjectMap : list) {
            bw.write(stringObjectMap.toString());
            bw.newLine();
            System.out.println(stringObjectMap);
        }
        bw.flush();
        bw.close();
    }
//    查记录条数
    @Test
    public void test8(){
        String sql = "select count(id) from emp";
        Long total = template.queryForObject(sql, Long.class);
        System.out.println(total);
    }
//    查询所有的记录条目并且将其封装成为Emp对象的List集合
    @Test
    public  void Test9test() {
        String sql="select * from emp";
        List <Emp> list=template.query(sql,new BeanPropertyRowMapper<Emp>(Emp.class));
        for(Emp emp:list){
            System.out.println(emp);
        }
    }

}
